package com.briup.estore.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 编码过滤器
 */
@WebFilter(value = "/*", initParams = {
        @WebInitParam(name = "encoding", value = "utf-8")
})
public class EncodingFilter implements Filter {
    private String encoding;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("encoding");
        if (encoding == null) {
            encoding = "utf-8";
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //编码
        //如果于丹css文件js文件就放行
        HttpServletRequest req = (HttpServletRequest) request;
        if (req.getRequestURI().contains(".css") || req.getRequestURI().contains(".js")) {
            chain.doFilter(request, response);
        }
        request.setCharacterEncoding(encoding);
        response.setCharacterEncoding(encoding);
        //response.setContentType("text/html;charset="+encoding);
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
